package com.project.service.impl;

import com.project.bean.StudentBean;
import com.project.mapper.IStudentMapper;
import com.project.service.IStudentService;
import org.apache.ibatis.session.SqlSession;

import java.util.List;

public class StudentServiceImpl extends BaseService implements IStudentService {

    @Override
    public void add(StudentBean student) {
        SqlSession session = this.getSession();
        IStudentMapper mapper = session.getMapper(IStudentMapper.class);
        mapper.add(student);
        session.commit();
        session.close();
    }

    @Override
    public void de(Integer id) {
        SqlSession session = this.getSession();
        IStudentMapper mapper = session.getMapper(IStudentMapper.class);
        mapper.de(id);
        session.commit();
        session.close();
    }

    @Override
    public List<StudentBean> studentList(String name, String className, String roomAddress) {
        SqlSession session = this.getSession();
        IStudentMapper mapper = session.getMapper(IStudentMapper.class);
        List<StudentBean> list = mapper.studentList(name,className,roomAddress);
        session.close();
        return list;
    }

    @Override
    public StudentBean findById(Integer id) {
        SqlSession session = this.getSession();
        IStudentMapper mapper = session.getMapper(IStudentMapper.class);
        StudentBean stu = mapper.findById(id);
        session.close();
        return stu;
    }

    @Override
    public int countByClass(Integer classId) {
        SqlSession session = this.getSession();
        IStudentMapper mapper = session.getMapper(IStudentMapper.class);
        int num = mapper.countByClass(classId);
        session.close();
        return num;
    }

    @Override
    public int countByRoom(Integer roomId) {
        SqlSession session = this.getSession();
        IStudentMapper mapper = session.getMapper(IStudentMapper.class);
        int num = mapper.countByRoom(roomId);
        session.close();
        return num;
    }

    @Override
    public void changeRoom(Integer studentId, Integer roomId) {
        SqlSession session = this.getSession();
        IStudentMapper mapper = session.getMapper(IStudentMapper.class);
        mapper.changeRoom(studentId,roomId);
        session.commit();
        session.close();
    }

    @Override
    public List<StudentBean> findByRoom(int roomId) {
        SqlSession session = this.getSession();
        IStudentMapper mapper = session.getMapper(IStudentMapper.class);
        List<StudentBean> list = mapper.findByRoom(roomId);
        session.close();
        return list;
    }
}
